<!doctype html>
<html lang="zh">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>登录界面</title>
	<link rel="stylesheet" type="text/css" href="/static/webAPP/css/loginstyles.css">
	<!--[if IE]>
		<script src="http://libs.baidu.com/html5shiv/3.7/html5shiv.min.js"></script>
	<![endif]-->
</head>
<body>
	<div class="jq22-container">
		<div class="login-wrap">
			<div class="login-html">
				<input id="tab-1" type="radio" name="tab" class="sign-in" checked><label for="tab-1" class="tab">登录</label>
				<input id="tab-2" type="radio" name="tab" class="sign-up"><label for="tab-2" class="tab">注册</label>
				<div class="login-form">
					<div class="sign-in-htm">
						<div class="group">
							<label for="user" class="label">账号</label>
							<input id="signIn_user" type="text" class="input">
						</div>
						<div class="group">
							<label for="pass" class="label">密码</label>
							<input id="signIn_password" type="password" class="input" data-type="password">
						</div>
						<div class="group">
							<input id="check" type="checkbox" class="check" checked>
							<label for="check"><span class="icon"></span>保存密码</label>
						</div>
						<div class="group">
							<input type="submit" class="button signinIn_btn" value="登录">
						</div>
						<div class="hr"></div>
						<div class="foot-lnk">
							<a href="#forgot">忘记密码?</a>
						</div>
					</div>
					<div class="sign-up-htm">
<!--						<div class="group">-->
<!--							<label for="user" class="label">账号</label>-->
<!--							<input id="signUp_user" type="text" class="input" placeholder="账号由6-18位字母或数字">-->
<!--						</div>-->
<!--						<div class="group">-->
<!--							<label for="pass" class="label">密码</label>-->
<!--							<input id="signUp_password" type="password" class="input" data-type="password" placeholder="密码由字母开头的6-18字母或数字">-->
<!--						</div>-->
<!--						<div class="group">-->
<!--							<label for="pass" class="label">再次输入密码</label>-->
<!--							<input id="signUp_password1" type="password" class="input" data-type="password" placeholder="两次密码必须相同">-->
<!--						</div>-->
<!--						<div class="group">-->
<!--							<label for="pass" class="label">手机号</label>-->
<!--							<input id="signUp_phone" type="text" class="input" placeholder="十一位手机号">-->
<!--						</div>-->
						<div class="group">
							<button class="button signUp_btn" >使用微信账号注册</button>
						</div>
<!--						<div class="hr"></div>-->
<!--						<div class="foot-lnk">-->
<!--							<label for="tab-1">已有账号?</a>-->
<!--						</div>-->
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<script src="/static/webAPP/js/jquery-1.8.3.min.js"></script>
	<script src="/static/webAPP/lib/mobile/layer.js"></script>
	<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=DouIq9pM7lqTiVOnzOQ6Q8biwgi2GIM0&s=1">
		//v2.0版本的引用方式：src="http://api.map.baidu.com/api?v=2.0&ak=您的密钥"
		</script>
	<script>
	$(function(){
		var userAgent = navigator.userAgent.toLowerCase();

		// if(userAgent.match(/MicroMessenger/i) == "micromessenger") {
		// 	$(".foot-lnk").html('<a href="javascript:void(0);" class="weichatLogup">点击微信注册</a>')
		// }

		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r){
			if(this.getStatus() == BMAP_STATUS_SUCCESS){
				$.ajax({
					url:'https://api.map.baidu.com/geocoder/v2/?ak=Ya2nSaqjT3vNrIgba1v4nfWzSxGdtgZD&location=' + r.point.lat + ',' + r.point.lng + '&output=json&pois=1&s=1',
					dataType: "jsonp",
					callback: 'BMap._rd._cbk43398',
					success: function(mag){
						var city = mag.result.addressComponent.city
						var citycode = mag.result.cityCode
						sessionStorage.setItem("citycode", citycode);
						sessionStorage.setItem("city", city);
					}
				})
			}
			else {
				sessionStorage.setItem("citycode", '127');
				sessionStorage.setItem("city", '合肥市');
				layer.open({
					content: '定位失败,默认为合肥市',
					skin: 'msg',
					time: 3 //2秒后自动关闭
				});
			}
		},{enableHighAccuracy: true})

		$(".signUp_btn").on("click",function(){
			var code = getUrlParam("code")
			$.ajax({
				type: "get",
					//TODO:
				url: "/Api/Web/wxSignUp",
				data: {code: code},
				dataType: "json",
				success: function(mag){
					window.location = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx45db0b3d36e1f241&redirect_uri=https%3A%2F%2Fvm.fowotek.com%2Fstatic%2FwebAPP%2Fweichatlogin.html&response_type=code&scope=snsapi_userinfo&#wechat_redirect";
				},
				error: function(){
					layer.open({
						content: '微信注册失败，请退出重试',
						skin: 'msg',
						time: 2 //2秒后自动关闭
					});
				}
			})
		})

		$(".signinIn_btn").on("click",function(){
			var username = $("#signIn_user").val()
			var password = $("#signIn_password").val()
			var code = getUrlParam("code")
			if (isnull(username) || isnull(password)) {
				layer.open({
					content: '账号、密码不能为空',
					skin: 'msg',
					time: 2 //2秒后自动关闭
				});
			}else{
				if (sessionStorage.getItem("sessionName") == username) {
					window.location = "/static/webAPP/personal-center.html";
					layer.open({
						content: '账号已登录',
						skin: 'msg',
						time: 2 //2秒后自动关闭
					});
				}else{
					$.ajax({
						type: "post",
                //TODO:
						url: "/signIn",
						data: {username: username,password: password},
						dataType: "json",
						success:function(data){
							if (data.data.name == undefined || data.data.name == null || data.data.name == '') {
								var username = ''
							}else{
								var username = data.data.name
							}

							if (data.data.deliveryAddress == undefined || data.data.deliveryAddress == null || data.data.deliveryAddress == '') {
								var usermyaddress = ''
								var usermyaddressid = ''
							}else{
								var usermyaddress = data.data.deliveryAddress.address
								var usermyaddressid = data.data.deliveryAddress.id
							}

							if (data.data.machine == undefined || data.data.machine == null || data.data.machine == '') {
								var useraddressid = ''
								var useraddressplace = ''
							}else{
								var useraddressid = data.data.machine.id
								var useraddressplace = data.data.machine.place
							}
							if (data.data.membership == undefined || data.data.membership == null || data.data.membership == '') {
								var usermembershiplevel = 0
                                var usermembershipremaining = ''
							}else{
								var usermembershiplevel = data.data.membership.level
								var usermembershipremaining = data.data.membership.remaining_amount
							}
							sessionStorage.setItem("sessionMembershiplevel", usermembershiplevel)
							sessionStorage.setItem("sessionMembershipremaining", usermembershipremaining)
							sessionStorage.setItem("sessionName", username)
							sessionStorage.setItem("sessionAddress", usermyaddress)
							sessionStorage.setItem("addressid", usermyaddressid)
							sessionStorage.setItem("machineplaceId", useraddressid)
							sessionStorage.setItem("machineplace", useraddressplace)
							sessionStorage.setItem("sessionUserid", data.data.id)

							if (data.code == 'success') {
								var userAgent = navigator.userAgent.toLowerCase();
								if(userAgent.match(/MicroMessenger/i) == "micromessenger") {
									layer.open({
										content: '是否绑定微信号？',
										btn: ['绑定', '不要'],
										shadeClose: false,
										yes: function(index){
										// window.location = "./login.html";
											$.ajax({
												type: "get",
													//TODO:
												url: "/Api/Web/bindWXUserInfo",
												data: {code: code},
												dataType: "json",
												success: function(mag){
													window.location = "/static/webAPP/personal-center.html";
												},
												error: function(){
													layer.open({
														content: '绑定失败，稍后重试',
														skin: 'msg',
														time: 2 //2秒后自动关闭
													});
												}
											})
										},
										btn2: function(index){
											window.location = "/static/webAPP/personal-center.html";
										}
									});
								}else{
									//页面跳转
									window.location = "/static/webAPP/personal-center.html";
								}

							}else {
								layer.open({
									content: data.msg,
									skin: 'msg',
									time: 2 //2秒后自动关闭
								});
							}
						},
						error: function(){
							layer.open({
								content: '登录失败,稍后重试',
								skin: 'msg',
								time: 2 //2秒后自动关闭
							});
						}
					})
				}
			}
		})

	// 	$(".signUp_btn").on("click",function(){
	// 		var userName = $("#signUp_user").val()
	// 		var passWord = $("#signUp_password").val()
	// 		var phone = $("#signUp_phone").val()
	// 		//判断输入手机号
	// 		var reg_phone = /^1(3[0-9]|4[5,7]|5[0,1,2,3,5,6,7,8,9]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\d{8}$/
	// 		if (phone != '' && phone != undefined && phone != null) {
	// 			if(!reg_phone.test(phone)){   //验证手机是否符合格式
	// 				layer.open({
	// 					content: '请输入正确的手机号',
	// 					skin: 'msg',
	// 					time: 2 //2秒后自动关闭
	// 				});
	// 				return
	// 			}
	// 		}
	//
	// 		var reg_password = /^[a-zA-Z][0-9a-zA-Z]{6,18}$/
	// 		var reg_username = /^[A-Za-z0-9]{6,18}$/
	// 		if (isnull(userName) || isnull(passWord)) {
	// 			layer.open({
	// 				content: "账号，密码均不能为空",
	// 				skin: 'msg',
	// 				time: 2 //2秒后自动关闭
	// 			})
	// 			return
	// 		}else if (reg_password.test(passWord) && reg_username.test(userName)) {
	// 			if (passWord != $("#signUp_password1").val()) {
	// 				layer.open({
	// 					content: '两次密码输入不一致',
	// 					skin: 'msg',
	// 					time: 2 //2秒后自动关闭
	// 				});
	// 				return
	// 			}else{
	// 				$.ajax({
	// 					type: "post",
    //             //TODO:
	// 					url: "/signUp",
	// 					data: {username: userName,password: passWord,phone: phone},
	// 					dataType: "json",
	// 					success:function(data){
	// 						if (data.code == 10000) {
	// 							window.location = "/static/webAPP/index.html";
	// 							sessionStorage.setItem("sessionName", userName)
	// 							if (data.data.name == undefined || data.data.name == null || data.data.name == '') {
	// 								var username = ''
	// 							}else{
	// 								var username = data.data.name
	// 							}
	//
	// 							if (data.data.deliveryAddress == undefined || data.data.deliveryAddress == null || data.data.deliveryAddress == '') {
	// 								var usermyaddress = ''
	// 								var usermyaddressid = ''
	// 							}else{
	// 								var usermyaddress = data.data.deliveryAddress.address
	// 								var usermyaddressid = data.data.deliveryAddress.id
	// 							}
	//
	// 							if (data.data.machine == undefined || data.data.machine == null || data.data.machine == '') {
	// 								var useraddressid = ''
	// 								var useraddressplace = ''
	// 							}else{
	// 								var useraddressid = data.data.machine.id
	// 								var useraddressplace = data.data.machine.place
	// 							}
	// 							if (data.data.membership == undefined || data.data.membership == null || data.data.membership == '') {
	// 								var usermembershiplevel = 0
    //                                 var usermembershipremaining = ''
	// 							}else{
	// 								var usermembershiplevel = data.data.membership.level
	// 								var usermembershipremaining = data.data.membership.remaining_amount
	// 							}
	// 							sessionStorage.setItem("sessionMembershiplevel", usermembershiplevel)
	// 							sessionStorage.setItem("sessionMembershipremaining", usermembershipremaining)
	// 							sessionStorage.setItem("sessionName", username)
	// 							sessionStorage.setItem("sessionAddress", usermyaddress)
	// 							sessionStorage.setItem("addressid", usermyaddressid)
	// 							sessionStorage.setItem("machineplaceId", useraddressid)
	// 							sessionStorage.setItem("machineplace", useraddressplace)
	// 							sessionStorage.setItem("sessionUserid", data.data.id)
	// 						}else{
	// 							layer.open({
	// 								content: data.message,
	// 								skin: 'msg',
	// 								time: 2 //2秒后自动关闭
	// 							});
	// 						}
	// 					},
	// 					error:function(){
	// 						layer.open({
	// 								content: '服务器错误，请稍后注册',
	// 								skin: 'msg',
	// 								time: 2 //2秒后自动关闭
	// 							});
	// 					}
	// 				})
	// 			}
	// 		}else{
	// 			layer.open({
	// 				content: "账号，密码由6-18位字母或数字,不能含有空格和特殊字符",
	// 				skin: 'msg',
	// 				time: 2 //2秒后自动关闭
	// 			})
	// 			return
	// 		}
	// 	})
	})


		function isnull(val) {

			var str = val.replace(/(^\s*)|(\s*$)/g, '');//去除空格;

			if (str =='' || str ==undefined || str ==null) {
				return true;
			} else {
				return false;
			}
		}

		function getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
			var context = "";
			if (r != null){
				context = r[2];
				reg = null;
				r = null;
			}
			return context == null || context == "" || context == "undefined" ? "" : context;
		}

	</script>


</body>
</html>